// Copyright (C) Kumo inc. and its affiliates.
// Author: Jeff.li lijippy@163.com
// All rights reserved.
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.
//


#include <melon/benchmark.h>
#include <melon/init/init.h>

#include <pollux/benchmarks/ExpressionBenchmarkBuilder.h>
#include <pollux/functions/lib/benchmarks/FunctionBenchmarkBase.h>
#include <pollux/functions/prestosql/registration/registration_functions.h>
#include <pollux/type/type.h>
#include <pollux/vector/base_vector.h>
#include <pollux/vector/complex_vector.h>
#include <pollux/vector/decoded_vector.h>

using namespace kumo::pollux;

int main(int argc, char** argv) {
  melon::Init init(&argc, &argv);

  ExpressionBenchmarkBuilder benchmarkBuilder;
  kumo::pollux::functions::prestosql::registerAllScalarFunctions();

  auto* pool = benchmarkBuilder.pool();
  auto& vm = benchmarkBuilder.vectorMaker();

  auto createSet =
      [&](const TypePtr& elementType,
          const std::vector<int>& containerLengths = {5, 10, 20, 40}) {
        VectorFuzzer::Options options;
        options.vectorSize = 1000;
        options.complexElementsMaxSize = 1'000'000;
        options.containerVariableLength = false;

        for (auto length : containerLengths) {
          options.containerLength = length;
          options.stringLength = length;
          VectorFuzzer fuzzer(options, pool);

          std::vector<VectorPtr> columns;
          for (int i = 0; i < 4; i++) {
            columns.push_back(fuzzer.fuzz(ARRAY(elementType)));
          }

          benchmarkBuilder
              .addBenchmarkSet(
                  fmt::format(
                      "array_concat_{}_{}",
                      elementType->toString(),
                      options.containerLength),
                  vm.row_vector(columns))
              .addExpression("2arg", "concat(c0, c1)")
              .addExpression("3arg", "concat(c0, c1, c2)")
              .addExpression("4arg", "concat(c0, c1, c2, c3)");
        }
      };

  createSet(INTEGER());
  createSet(VARCHAR());
  createSet(BOOLEAN());

  benchmarkBuilder.registerBenchmarks();

  melon::runBenchmarks();
  return 0;
}
